package com.kaikeba.controller;

import com.kaikeba.bean.*;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.DateformatUtil;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierController {
    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        //获得数据
        List<Map<String, Integer>> data = CourierService.console();
        Message msg = new Message();
        if (data.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //获取偏移值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //每页显示的数量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));

        //查询
        List<Courier> list1 = CourierService.findAll(true, offset, pageNumber);
        //转换格式
        List<BootStrapTableCourier> list2 = new ArrayList<>();
        for (Courier c : list1) {
            String regTime = DateformatUtil.format(c.getRegTime());
            String lastLoginTime = DateformatUtil.format(c.getLastLoginTime());
            BootStrapTableCourier c2 = new BootStrapTableCourier(c.getId(), c.getCourierName(), c.getCourierPhone(),"******",c.getPassword() ,c.getExpressSum(), regTime, lastLoginTime);
            list2.add(c2);
        }
        //将集合封装为bootstrap-table识别的格式
        //获得总数
        List<Map<String, Integer>> console = UserService.console();
        Integer total = console.get(0).get("data_size");
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/courier/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String courierName = request.getParameter("courierName");
        String courierPhone = request.getParameter("courierPhone");
        String number = request.getParameter("number");
        String password = request.getParameter("password");
        Courier courier = new Courier(courierName, courierPhone, number, password);
        //插入数据
        boolean flag = CourierService.add(courier);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("快递员信息录入成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("快递员信息录入失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String courierPhone = request.getParameter("courierPhone");
        //获得结果
        Courier courier = CourierService.findByCourierPhone(courierPhone);
        Message msg = new Message();
        if (courier == null) {
            msg.setStatus(-1);
            msg.setResult("用户不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(courier);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {
        //得到参数
        int id = Integer.parseInt(request.getParameter("id"));
        String courierName = request.getParameter("courierName");
        String courierPhone = request.getParameter("courierPhone");
        String number = request.getParameter("number");
        String password = request.getParameter("password");
        Courier newCourier = new Courier(courierName,courierPhone,number,password);
        newCourier.setId(id);
        boolean flag = CourierService.updateById(newCourier);
        Message msg = new Message();
        if (flag == true) {
            msg.setStatus(0);
            msg.setResult("修改成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        String courierPhone = request.getParameter("courierPhone");
        boolean flag = CourierService.deleteByCourierPhone(courierPhone);
        Message msg = new Message();
        if (flag == true) {
            msg.setStatus(0);
            msg.setResult("删除成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
